// import Cookies from 'js-cookie';
import { stringify } from 'qs';
import CryptoJS from "crypto-js";
import { request, getAccessToken, getUserInfo, encrypt }  from '../utils/request';
import { getConfig } from '../../config';
const config = getConfig();


export async function setUserCooike(result) {
  if (result.token) {
    // 开发环境下写cookie方便测试
    // if (config.useFrontCookie) {
      console.log(result.__ywweb_user_info)
      // Cookies.set('__ywweb_access_token', result.__ywweb_access_token, {expires: 1});
      sessionStorage.setItem("__ywweb_access_token",result.__ywweb_access_token);
      sessionStorage.setItem("__ywweb_user_info",encrypt(result.__ywweb_user_info));
      sessionStorage.setItem("__ywweb_user_roles",encrypt(result.__ywweb_user_roles));
      // Cookies.set('__ywweb_access_token', result.__ywweb_access_token);
      // Cookies.set('__ywweb_user_info', encodeURIComponent(JSON.stringify(result.__ywweb_user_info)));
      // Cookies.set('__ywweb_user_roles', encodeURIComponent(JSON.stringify(result.__ywweb_user_roles)));
    // }
  }
}

export async function removeUserCooike() {
  sessionStorage.clear();
  // if (getAccessToken()) {
    // 开发环境下写cookie方便测试
    // if (config.useFrontCookie) {
    //   Cookies.remove('__ywweb_access_token');
    //   Cookies.remove('__ywweb_user_info');
    //   Cookies.remove('__ywweb_user_roles');
    // }
  // }
}


export async function updateUserCooike(params) {
  console.log('params',params)
  sessionStorage.setItem("__ywweb_user_info",encrypt({...getUserInfo(),...params}));
}

//获取用户个人信息----------------------------------------------------------------
export async function queryCurrent() {
  return getUserInfo();
}

//账号登录
export async function fakeAccountLogin(params) {
  return request('/sys/login', {
    method: 'POST',
    body: params,
  });
}

// 登出
export async function fakeLogOut(params) {
  return request('/sys/logout', {
    method: 'POST',
    body: params,
  });
}



//修改用用户详情
export async function fakeUserInfo(params) {
  return request('/sys/user/updateSelfInfo', {
    method: 'POST',
    body: params,
  });
}

//提交修改密码
export async function fakeModifyPsw(params) {
  return request('/sys/user/password', {
    method: 'POST',
    body: params,
  });
}

// 用户详情
export async function queryUserInfo(params) {
  if (config.isMock) {
    return request(`/sys/user/info`, {
      method: 'POST',
      body: params,
    });
  }else{
    return request(`/sys/user/info/${params.userId}`, {
      method: 'POST',
    });
  }
}


// 管理员获取所有用户列表
export async function queryAllUsers(params) {
  return request('/sys/user/list', {
    method: 'POST',
    body: params,
  });
}

// 管理员用户删除
export async function saveUsersDel(params) {
  return request('/sys/user/delete', {
    method: 'POST',
    body: params,
  });
}


// 管理员新增用户
export async function saveUsersAdd(params) {
  return request('/sys/user/save', {
    method: 'POST',
    body: params,
  });
}


// 管理员修改用户
export async function saveUsersEdit(params) {
  return request('/sys/user/update', {
    method: 'POST',
    body: params,
  });
}
